using System;
using System.Collections.Generic;
using ArmyManager.Items;
using ArmyManager.Units;

namespace ArmyManager.Commands
{
	public class ReplaceCommand<T> : BasicCommand
	{
		private readonly Func<T, T> _replaceFunction;
		private readonly T _newObject;

		public T OldObject { get; private set; }

		public ReplaceCommand(Func<T, T> replaceFunction, T newObject)
		{
			_replaceFunction = replaceFunction;
			_newObject = newObject;
		}

		protected override void ExecuteInternal()
		{
			OldObject = _replaceFunction(_newObject);
		}

		protected override void UndoInternal()
		{
			_replaceFunction(OldObject);
			OldObject = default(T);
		}
	}

	#region ReplaceCommand Specializations

	public class ReplaceArmoryItemsCommand : ReplaceCommand<List<Item>>
	{
		public ReplaceArmoryItemsCommand(Armory armory, List<Item> newItems)
			: base(armory.ReplaceItems, newItems)
		{ }
	}

	public class ReplaceUnitItemCommand : ReplaceCommand<Item>
	{
		public ReplaceUnitItemCommand(Unit unit, Item newItem)
			: base(unit.ReplaceItem, newItem)
		{ }
	}

	#endregion

}